<?php

class SendEmails extends MY_Controller {

	function __construct() {
		parent::__construct();
	}

	public function index(){
	}
	
	public function cron(){
		$config = array (
			'protocol' => 'smtp',
			'smtp_host' => 'ssl://smtp.googlemail.com',
			'smtp_port' => 465,
			'smtp_user' => 'thetrove.sg@gmail.com',
			'smtp_pass' => 'recyclingrocks',
			'mailtype'  => 'html'
		);
		
		$this->load->library('email',$config);
		$this->email->set_newline("\r\n");
		
		$this->load->model('emailgenerator_model');
		$data = $this->emailgenerator_model->get_emails();
		
		foreach($data AS $email){
			echo $email['fb_id'] . ' - ' . $email['email'];
			
			$this->email->from('thetrove.sg@gmail.com','The Trove');
			$this->email->to($email['email_id']);
			$this->email->subject('Newsletter from The Trove');
			
			$this->email->message($email['message']);
			
			if ($this->email->send()) {
				echo(' Success\n');
				$this->emailgenerator_model->delete_email($email['email_id']);
			} else {
				echo '\n';
				show_error($this->email->print_debugger());
				echo '\n';
			}
			
			
		}
	}
}